package antlr4.myantlrtest.test.fishprotocol.listener;


import antlr4.myantlrtest.test.fishprotocol.gen.FishProtocolBaseListener;
import antlr4.myantlrtest.test.fishprotocol.gen.FishProtocolParser;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.TreeMap;

public class ProtocolListener extends FishProtocolBaseListener {

    public static TreeMap<String, Object> protocolMap = new TreeMap<>();
    public static JSONArray vParams = new JSONArray();
    @Override
    public void enterProtocol(FishProtocolParser.ProtocolContext ctx) {
        protocolMap = new TreeMap<>();
        vParams = new JSONArray();
        String text = ctx.IntLiteral().getText();  //协议号
        protocolMap.put("k", "c" + text);
    }

    @Override
    public void exitProtocol(FishProtocolParser.ProtocolContext ctx) {
        protocolMap.put("v", vParams);
    }


    @Override
    public void enterIntLiteral(FishProtocolParser.IntLiteralContext ctx) {
        JSONObject param = new JSONObject();
        param.put("int", Convert.toInt(ctx.getText()));
        vParams.add(param);
    }

    @Override
    public void enterLongLiteral(FishProtocolParser.LongLiteralContext ctx) {
        JSONObject param = new JSONObject();
        param.put("long", Convert.toLong(ctx.getText()));
        vParams.add(param);
    }

    @Override
    public void enterFloatLiteral(FishProtocolParser.FloatLiteralContext ctx) {
        JSONObject param = new JSONObject();
        param.put("float",  Convert.toFloat(ctx.getText()));
        vParams.add(param);
    }

    @Override
    public void enterDoubleLiteral(FishProtocolParser.DoubleLiteralContext ctx) {
        JSONObject param = new JSONObject();
        param.put("double",  Convert.toDouble(ctx.getText()));
        vParams.add(param);
    }

    @Override
    public void enterStringLiteral(FishProtocolParser.StringLiteralContext ctx) {
        JSONObject param = new JSONObject();
        param.put("string", normalizing(ctx.getText()));
        vParams.add(param);
    }

    @Override
    public void enterBooleanLiteral(FishProtocolParser.BooleanLiteralContext ctx) {
        JSONObject param = new JSONObject();
        param.put("boolean", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterNullLiteral(FishProtocolParser.NullLiteralContext ctx) {
        vParams.add(null);//占空位
    }

    @Override
    public void enterIntArr(FishProtocolParser.IntArrContext ctx) {
        JSONObject param = new JSONObject();
        param.put("int[]", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterLongArr(FishProtocolParser.LongArrContext ctx) {
        JSONObject param = new JSONObject();
        param.put("long[]", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterFloatArr(FishProtocolParser.FloatArrContext ctx) {
        JSONObject param = new JSONObject();
        param.put("float[]", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterDoubleArr(FishProtocolParser.DoubleArrContext ctx) {
        JSONObject param = new JSONObject();
        param.put("double[]", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterStringArr(FishProtocolParser.StringArrContext ctx) {
        JSONObject param = new JSONObject();
        param.put("string[]", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterBoolArr(FishProtocolParser.BoolArrContext ctx) {
        JSONObject param = new JSONObject();
        param.put("boolean[]", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterIntEQ(FishProtocolParser.IntEQContext ctx) {
        JSONObject param = new JSONObject();
        param.put("int",  Convert.toInt(ctx.getText()));
        vParams.add(param);
    }

    @Override
    public void enterLongEQ(FishProtocolParser.LongEQContext ctx) {
        JSONObject param = new JSONObject();
        param.put("long",  Convert.toLong(ctx.getText()));
        vParams.add(param);
    }

    @Override
    public void enterFloatEQ(FishProtocolParser.FloatEQContext ctx) {
        JSONObject param = new JSONObject();
        param.put("float", Convert.toFloat(ctx.getText()));
        vParams.add(param);
    }

    @Override
    public void enterDoubleEQ(FishProtocolParser.DoubleEQContext ctx) {
        JSONObject param = new JSONObject();
        param.put("double",  Convert.toDouble(ctx.getText()));
        vParams.add(param);
    }

    @Override
    public void enterStringEQ(FishProtocolParser.StringEQContext ctx) {
        JSONObject param = new JSONObject();
        param.put("string", normalizing(ctx.getText()));
        vParams.add(param);
    }

    @Override
    public void enterBoolEQ(FishProtocolParser.BoolEQContext ctx) {
        JSONObject param = new JSONObject();
        param.put("boolean", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterIntArrEQ(FishProtocolParser.IntArrEQContext ctx) {
        JSONObject param = new JSONObject();
        param.put("int[]", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterLongArrEQ(FishProtocolParser.LongArrEQContext ctx) {
        JSONObject param = new JSONObject();
        param.put("long[]", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterFloatArrEQ(FishProtocolParser.FloatArrEQContext ctx) {
        JSONObject param = new JSONObject();
        param.put("float[]", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterDoubleArrEQ(FishProtocolParser.DoubleArrEQContext ctx) {
        JSONObject param = new JSONObject();
        param.put("double[]", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterStringArrEQ(FishProtocolParser.StringArrEQContext ctx) {
        JSONObject param = new JSONObject();
        param.put("string[]", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterBoolArrEQ(FishProtocolParser.BoolArrEQContext ctx) {
        JSONObject param = new JSONObject();
        param.put("boolean[]", ctx.getText());
        vParams.add(param);
    }

    @Override
    public void enterNetObject(FishProtocolParser.NetObjectContext ctx) {
        JSONObject param = new JSONObject();
        String[] split = ctx.getText().split("=");
        if(split.length == 2){
            String type = split[0];
            String string = split[1];
            String normalizing = normalizing(string);
            JSONObject object = JSON.parseObject(normalizing);
            param.put(type, object);
        }
        vParams.add(param);
    }


    public static String normalizing(String string){
        String str = string;
        if (string.startsWith("'") && string.endsWith("'")) {
            str = string.substring(1, string.length() - 1);
        }
        if (string.startsWith("\"") && string.endsWith("\"")) {
            str = string.substring(1, string.length() - 1);
        }
        return str;
    }
}
